package com.it.base.core.system.user.service.impl;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.it.base.common.view.PageResult;
import com.it.base.core.system.user.mapper.SysUserMapper;
import com.it.base.core.system.user.model.entity.SysUser;
import com.it.base.core.system.user.model.from.SysUserSelectPageListFormDTO;
import com.it.base.core.system.user.model.from.SysUserValidatorFormDTO;
import com.it.base.core.system.user.service.ISysUserService;
import com.it.base.support.string.StringUtil;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public PageResult<SysUser> selectPageList(@Valid SysUserSelectPageListFormDTO sysUserSelectPageListFormDTO) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();

        if (StringUtil.isNotBlank(sysUserSelectPageListFormDTO.getUserName())) {
            queryWrapper.lambda().eq(SysUser::getUserName, sysUserSelectPageListFormDTO.getUserName());
        }
        if (StringUtil.isNotBlank(sysUserSelectPageListFormDTO.getNickName())) {
            queryWrapper.lambda().eq(SysUser::getNickName, sysUserSelectPageListFormDTO.getNickName());
        }
        if (StringUtil.isNotBlank(sysUserSelectPageListFormDTO.getPhone())) {
            queryWrapper.lambda().eq(SysUser::getPhone, sysUserSelectPageListFormDTO.getPhone());
        }
        if (StringUtil.isNotBlank(sysUserSelectPageListFormDTO.getEmail())) {
            queryWrapper.lambda().eq(SysUser::getEmail, sysUserSelectPageListFormDTO.getEmail());
        }
        if (StringUtil.isNotBlank(sysUserSelectPageListFormDTO.getSex())) {
            queryWrapper.lambda().eq(SysUser::getSex, sysUserSelectPageListFormDTO.getSex());
        }
        if (sysUserSelectPageListFormDTO.getStatus() != null) {
            queryWrapper.lambda().eq(SysUser::getStatus, sysUserSelectPageListFormDTO.getStatus());
        }

        IPage<SysUser> page = sysUserMapper.selectPage(
                new Page<>(sysUserSelectPageListFormDTO.getPageNo(), sysUserSelectPageListFormDTO.getPageSize()),
                queryWrapper);
        return new PageResult<>(page.getTotal(), page.getRecords());
    }

    @Override
    public boolean validatorExist(SysUserValidatorFormDTO sysUserValidatorFormDTO) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUser::getUserName, sysUserValidatorFormDTO.getUserName());
        return this.count(queryWrapper) > 0;
    }

}
